#include <glog/logging.h>
#include "gen-cpp/ChatService.h"
#include "client/ChatServiceClientHandler.h"
#include "client/SendMsgCallable.h"
#include "client/ReceiveMsgCallable.h"
#include "client/GenMsgCallable.h"
#include "client/RequestQueue.h"

#include <string>
#include <exception>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TDebugProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/THttpClient.h>
#include <thrift/TProcessor.h>
#include <glog/logging.h>
namespace at = apache::thrift;
namespace atp = apache::thrift::protocol;
namespace att = apache::thrift::transport;
int main() {
  const std::string host("localhost");
  const std::string path("/");
  const std::string http_header_host("www.cfood.com");
  int port = 9090;
 
  boost::shared_ptr<att::TTransport> socket(new att::TSocket(host, port));
  boost::shared_ptr<att::TTransport> trans(new att::TFramedTransport(socket));
  boost::shared_ptr<att::TTransport> http(new att::THttpClient(trans, http_header_host, "/"));
  boost::shared_ptr<atp::TTransport> receive_http(new att::THttpClient(socket, http_header_host, "/"));
  boost::shared_ptr<atp::TProtocol> prot(new atp::TBinaryProtocol(http));

  boost::shared_ptr<atp::TProtocol> receive_prot(new atp::TBinaryProtocol(receive_http));
  http->open();
  boost::shared_ptr<chat::ChatServiceClient> client(new chat::ChatServiceClient(prot));
  client::RequestQueue::Create(client);
  boost::shared_ptr<chat::ChatServiceClientHandler> handler(new chat::ChatServiceClientHandler()); 
  boost::shared_ptr<chat::ChatServiceProcessor> processor(new chat::ChatServiceProcessor(handler));

  boost::shared_ptr<boost::thread> send_thread(new boost::thread(client::SendMsgCallable()));
  boost::shared_ptr<boost::thread> receive_thread(new boost::thread(client::ReceiveMsgCallable(processor, receive_prot)));
  boost::shared_ptr<boost::thread> gen_msg_thread(new boost::thread(client::GenMsgCallable(1)));
  send_thread->join();
  receive_thread->join();
  gen_msg_thread->join();
  return 0;
}
